/*
 * show.js
 */

$(function(){
    var start = 0;
    var limit = 10;
    var loading = false;
    var end = false;
    var more = $('#more');

    function getList(pageStart, pageSize){
        more.text('加载中');
        loading = true;
        $.ajax({
            url: config.api.lists,
            data: {
                start: pageStart || start,
                limit: pageSize || limit,
                enable: 1
            }
        }).then(function(ret){
            loading = false;
            more.text('查看更多');
            if(!ret.data || ret.data.length === 0){
                end = true;
                more.text('没有更多了');
                return;
            }

            var html = Mustache.render($('#tmpl-list').html(), {list: ret.data});

            $('#lists').append($(html));
            start = start + (pageSize || limit);



            setTimeout(function(){
                resize();
            }, 1000)
        }, function(err){
            console.log(err);
            loading = false;
        })
    }

    function resize(){
        var lists = $('#lists li a');
        var ret = [];
        //for(var i = 0 ;i < lists.length; i= i+2){
        //    var list1 = lists[i];
        //    var list2 = lists[i+1] || null;
        //    if(!list2){
        //        continue;
        //    }
        //    var h1 = $(list1).find('img').height();
        //    var h2 = $(list2).find('img').height();
        //    var max = Math.max(h1, h2);
        //    $(list1).css('height', max + 'px');
        //    $(list2).css('height', max + 'px');
        //}
        var max = 0;
        for(var i = 0 ;i < lists.length; i++){
            var list = lists[i];
            var h = $(list).find('img').height();
            if(h>max){
                max = h;
            }
        }
        lists.css('height', max + 'px');

    }

    function getListByMobile(mobile){
        console.log(mobile)
        more.text('加载中');
        loading = true;
        $.ajax({
            url: config.api.listsByMobile + mobile,
            data: {
                activityId: 1
            }
        }).then(function(ret){
            loading = false;
            if(!ret.data){
                end = true;
                more.text(ret.message || '未找到');
                return;
            }else{
                more.hide();
            }
            console.log(ret);

            var html = Mustache.render($('#tmpl-list').html(), {list: [ret.data]});

            $('#lists').html($(html));
        }, function(err){
            console.log(err);
            loading = false;
            return;

            if(!ret.data){
                end = true;
                more.text(ret.message || '未找到');
                return;
            }else{
                more.hide();
            }

            var html = Mustache.render($('#tmpl-list').html(), {list: [ret.data]});

            $('#lists').html($(html));
        })
    }

    function dialog(msg){
        $('.dialog .dialog-cnt').text(msg);
        $('.dialog').show();
    }
    $('.dialog .dialog-ft').on('click', function(e){
        $('.dialog').hide();
    });

    more.on('click', function(e){
        if(loading || end){
            return false;
        }
        getList();
        return false;
    })

    // search
    $('#search').on('click', function(e){
        var mobile = $('input[name="search"]').val();
        console.log('search', mobile);
        if(!utils.mobile(mobile)){
            dialog('请输入正确的手机号码'); 
        }
        if(loading || end){
            return false;
        }
        getListByMobile(mobile);
        return false;
    })

    getList();
})
